﻿// JScript 文件
var IsKey = null;
function noresult()
{
    document.getElementById("ListArea").innerHTML="对不起,没有找到符合条件的商品";
}
function KeyInitPage()
{
    document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";	
    IsKey="1";
    initPage();
}
function H_InitPage()
{
    if(document.getElementById("tmpImg")!=null)
    {
        document.getElementById("tmpImg").style.display="block";
    }
    else
    {
        document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
    }
    IsKey="0";
    initPage();
}
// PageLoad function
  // This function is called when:
  // 1. after calling $.historyInit();
  // 2. after calling $.historyLoad();
  // 3. after pushing "Go Back" button of a browser
  function pageload(hash) {
    // hash doesn't contain the first # character.
   var step = "1"; //第几页
   var PageSize="12" ; //每页显示几个商品
   if(document.getElementById("PageSize") != null) 
    {
        PageSize = document.getElementById("PageSize").value;
    }
   var DisplayMethod="1" ;
   if(document.getElementById("DisplayMethod") != null) 
    {
        DisplayMethod = document.getElementById("DisplayMethod").value;
    }
   var Sort=null;
   var Order=null;
   var SortMethod=document.getElementById("SortMethod"); //排序
   if(SortMethod!=null)
   {
       if(SortMethod.selectedIndex>0)
       {
           Sort=SortMethod.options[SortMethod.selectedIndex].value;
           if(SortMethod.selectedIndex%2==0)
           {
               Order="2"; 
           }
           else
           {
               Order="1"; 
           }
       }
   }
   var key;
   if(IsKey=="1")
   {
       var url=window.location.href;
       var len=url.length;
       var index=url.indexOf('=');
       var index2=url.indexOf('#');
       if(index2>0)
       {
         len=index2;
       }
       key=url.substr(index+1,len-index-1);
   }
   var word=document.getElementById("txtKeyWord").value;
   var SearchMethod=document.getElementById("SearchMethod").options[document.getElementById("SearchMethod").selectedIndex].value;
   var PriceRange=0;
   var temp_Price=document.getElementById("PriceRange");
   if(temp_Price.selectedIndex>0)
   {
        PriceRange=temp_Price.options[temp_Price.selectedIndex].value;
   }   
   var Category="";
   var temp_ct=document.getElementById("ctl00_ContentPlaceHolder1_DDL_Category");
   if(temp_ct.selectedIndex>0)
   {
      Category=temp_ct.options[temp_ct.selectedIndex].value;  
   }
   var IsNew;
   if(document.getElementById("IsNew").checked)
   {
      IsNew="1";
   }
   else
   {
      IsNew="0";
   }
   var IsPromotion;
   if(document.getElementById("IsPromotion").checked)
   {
      IsPromotion="1" ; 
   }
   else
   {
      IsPromotion="0";
   }
   var IsCommend;
   if(document.getElementById("IsCommend").checked)
   {
        IsCommend="1";
   }
   else
   {
        IsCommend="0";
   }
    if(hash) {
      var paras = hash.split("-");      
      PageSize = decodeURIComponent(paras[0]);
      step = decodeURIComponent(paras[1]);
      DisplayMethod = paras[2];     
      Sort = paras[3];
      Order=paras[4];
      if(IsKey=="1")
      {
         $j.post("../Handler/SearchHandler.ashx", { Key: key,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsKey:IsKey},
            function(data){ 
                document.getElementById("ListArea").innerHTML= data;
            }
          );
      }
      else
      {
           $j.post("../Handler/SearchHandler.ashx", { Word: word,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsKey:IsKey,SearchMethod:SearchMethod,PriceRange:PriceRange,Category:Category,IsNew:IsNew,IsPromotion:IsPromotion,IsCommend:IsCommend},
            function(data){ 
                document.getElementById("ListArea").innerHTML= data;
            }
          );
      }
      
    } else {
        if(IsKey=="1")
        {
            $j.post("../Handler/SearchHandler.ashx", { Key: key,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsKey:IsKey},
                function(data){ 
                    document.getElementById("ListArea").innerHTML= data;
                }
            );
        }
        else
        {
             $j.post("../Handler/SearchHandler.ashx", { Word: word,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsKey:IsKey,SearchMethod:SearchMethod,PriceRange:PriceRange,Category:Category,IsNew:IsNew,IsPromotion:IsPromotion,IsCommend:IsCommend},
            function(data){ 
                document.getElementById("ListArea").innerHTML= data;
                }
             );
        }
    }
  }
function initPage()
 {
    // Initialize history plugin.
    // The callback is called at once by present location.hash. 
    $j.historyInit(pageload);    
    // set onlick event for buttons
    $j("a[@rel='history']").click(function(){
      // 
      var hash = this.href;
      hash = hash.replace(/^.*#/, '');
      // moves to a new page. 
      // pageload is called at once. 
      $j.historyLoad(hash);
      return false;
    });
}
//------改变展现方式--------------
function ChangeShowMethod(showmethod)
{   
    var ShowMethod;
    if(document.getElementById("DisplayMethod")!=null) 
    {
        ShowMethod=document.getElementById("DisplayMethod").value;
    }
    if(ShowMethod!=showmethod)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var PageSize="12";
        if(document.getElementById("PageSize") != null) 
        {
            PageSize = document.getElementById("PageSize").value;
        }
        var Page="1";
        if(document.getElementById("Page")!=null)
        {
            Page=document.getElementById("Page").value;
        }
       var Sort=null;
       var Order=null;
       var SortMethod=document.getElementById("SortMethod"); //排序
       if(SortMethod!=null)
       {
         if(SortMethod.selectedIndex>-1)
         {
           Sort=SortMethod.options[SortMethod.selectedIndex].value;
           if(SortMethod.selectedIndex%2==0)
           {
               Order="2"; 
           }
           else
           {
               Order="1"; 
           }
         }
       }
      loadCompoundURL(PageSize,Page,showmethod,Sort,Order); 
    }
}
//----------------------改变展现方式结束-----------------------
//---------------------改变显示数量------------------------------
function ChangePageSize(pagesize)
{
    var PageSize;
    if(document.getElementById("PageSize")!=null)
    {
        PageSize=document.getElementById("PageSize").value;
    }
    if(PageSize!=pagesize)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var ShowMethod;
        if(document.getElementById("DisplayMethod")!=null) 
        {
        ShowMethod=document.getElementById("DisplayMethod").value;
        }
        var Page="1";
        var Sort=null;
        var Order=null;
        var SortMethod=document.getElementById("SortMethod"); //排序
        if(SortMethod!=null)
        {
            if(SortMethod.selectedIndex>-1)
            {
                Sort=SortMethod.options[SortMethod.selectedIndex].value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
        }
        loadCompoundURL(pagesize,Page,ShowMethod,Sort,Order);
    }
}
//--------------------------改变页显示数量结束--------------------------------
//--------------------------排序---------------------------
function ChangeSort(sort)
{
    var Sort;
    var Order;
    var SortMethod=document.getElementById("SortMethod");
    if(SortMethod!=null)
    {
        if(SortMethod.selectedIndex>-1)
            {
                Sort=document.getElementById("Sort").value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
    }
    if(Sort!=sort)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var PageSize;
        if(document.getElementById("PageSize")!=null) 
        {
            PageSize=document.getElementById("PageSize").value;
        }
        var ShowMethod;
        if(document.getElementById("DisplayMethod")!=null) 
        {
            ShowMethod=document.getElementById("DisplayMethod").value;
        }
        var Page="1";
        loadCompoundURL(PageSize,Page,ShowMethod,sort,Order);
    }
}
//--------------------------排序结束--------------------
//-------------------------翻页-------------------------
function ChangePage(index,pagecount)
{
    var pattern=/^[0-9]*[1-9][0-9]*$/;
    var flag=pattern.test(index);
    if(!flag)
    {
        alert("请输入正整数");
        document.getElementById("turnPage").focus();
        return;
    }
    if(index<1) index=1;
    if(index>pagecount) index=pagecount;
    
    if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
    var Sort;
    var Order;
    var SortMethod=document.getElementById("SortMethod");
    if(SortMethod!=null)
    {
        if(SortMethod.selectedIndex>-1)
            {
                Sort=document.getElementById("Sort").value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
    }    
    var PageSize;
    if(document.getElementById("PageSize")!=null) 
    {
        PageSize=document.getElementById("PageSize").value;
    }
    var ShowMethod;
    if(document.getElementById("DisplayMethod")!=null) 
    {
        ShowMethod=document.getElementById("DisplayMethod").value;
    }
    var Page=index;
    loadCompoundURL(PageSize,Page,ShowMethod,Sort,Order);
}
//-------------------------翻页结束---------------------
function loadCompoundURL(ps,pg,smd,st,or)
   {
        var strUrl=ps; 
        strUrl +="-" + pg + "-" + smd + "-" + st+"-"+or;
        $j.historyLoad(strUrl);
   }
